package com.boarsoft.boar.demo.user;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.boarsoft.boar.demo.user.entity.PocAccount;
import com.boarsoft.boar.demo.user.entity.PocUser;
import com.boarsoft.boar.demo.user.service.PocAccountService;
import com.boarsoft.boar.demo.user.service.PocUserService;
import com.boarsoft.common.Util;

public class UserAppTestMain {
	private static final String dateFormate = "yyyy-MM-dd";
	private static ClassPathXmlApplicationContext ctx;

	public static void main(String[] args) {
		ctx = new ClassPathXmlApplicationContext("classpath:conf/context.xml");
		System.out.println("Startup ".concat(ctx.isRunning() ? "successfully." : "failed."));

		// accountTransferTest(ctx);
		accountReverseTest(ctx);
		// userListTest(ctx);
		// accountListTest(ctx);
		// userAddTest(ctx);
	}

	public static void userAddTest(ClassPathXmlApplicationContext ctx) {
		PocUserService pocUserService = ctx.getBean(PocUserService.class);
		// 插入tenantId为偶数的用户
		PocUser p = new PocUser();
		p.setUserId(666668);
		p.setUserName("赵六");
		p.setTenantId(12345);
		p.setMobile("1505055551");
		p.setCreateTime(Util.getStdmfDateTime());
		p.setModifyTime(Util.getStdmfDateTime());
		pocUserService.add(p.getTenantId(), p);
		System.out.println("User added");
		// 插入tenantId为奇数的用户
		p = new PocUser();
		p.setUserId(666669);
		p.setUserName("张三");
		p.setTenantId(12344);
		p.setMobile("1505055552");
		p.setCreateTime(Util.getStdmfDateTime());
		p.setModifyTime(Util.getStdmfDateTime());
		pocUserService.add(p.getTenantId(), p);
		System.out.println("User added");
	}

	public static void userListTest(ClassPathXmlApplicationContext ctx) {
		PocUserService pocUserService = ctx.getBean(PocUserService.class);
		PocUser pocUser = pocUserService.find(12344, 666666);
		System.out.println("用户信息：" + pocUser);
		pocUser = pocUserService.find(12345, 666667);
		System.out.println("用户信息：" + pocUser);
	}

	public static void accountListTest(ClassPathXmlApplicationContext ctx) {
		PocAccountService pocAccountService = ctx.getBean(PocAccountService.class);
		System.out.println("进入账户测试");
		PocAccount pocAccount = pocAccountService.find(12344, 666666);
		System.out.println("账户信息：" + pocAccount);
		pocAccount = pocAccountService.find(12345, 666666);
		System.out.println("账户信息：" + pocAccount);
	}

	public static void accountTransferTest(ClassPathXmlApplicationContext ctx) {
		PocAccountService pocAccountService = ctx.getBean(PocAccountService.class);
		boolean flag = pocAccountService.accountTransfer("0110101232301000010001", "106130121012348108",
				new BigDecimal("0.01"));
		if (flag == true) {
			System.out.println("转账成功");
		} else {
			System.out.println("转账失败");
		}
	}

	public static void accountReverseTest(ClassPathXmlApplicationContext ctx) {
		PocAccountService pocAccountService = ctx.getBean(PocAccountService.class);
		boolean flag = pocAccountService.accountReverse("0040828076", new SimpleDateFormat(dateFormate).format(new Date()));
		if (flag == true) {
			System.out.println("冲正成功");
		} else {
			System.out.println("冲正失败");
		}
	}
}
